{extend name="common@base/common" /} {block name="body"}
<link rel="stylesheet" href="__ROOT__/node/node_modules/xterm/dist/xterm.css"/>
<div class="page_message ">
    <section id="contents">
        <div id="terminal" style="width:100%; height:80vh;visibility:visible;overflow:auto"></div>
        <div id="loading" style="; margin: 10px;text-align: center; padding: 10px">正在执行中，请稍等<img src="__STATIC__/layui/css/modules/layer/default/loading-0.gif"></div>
        <div id="btn" style="display: none; margin: 10px;text-align: center;"><button class="layui-btn layui-btn-normal" onclick="closeBox()">关闭</button></div>
    </section>
</div>
<script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
<script src="__ROOT__/node/node_modules/xterm/dist/xterm.js"></script>
<script src="__ROOT__/node/node_modules/xterm/dist/addons/attach/attach.js"></script>
<script src="__ROOT__/node/node_modules/xterm/dist/addons/fit/fit.js"></script>
<script>
    $(function () {
        var term = new Terminal({
            cols: 80,
            rows: 25,
            disableStdin: true
        });
        term.open(document.getElementById('terminal'));

        // 连接服务端，workerman.net:2120换成实际部署web-msg-sender服务的域名或者ip
        var socket = io('http://{:SSH_IP}:2120');
        socket.on('connect', function () {
            socket.emit('login', 1);
            $.get("{$url}")
        });
        // 后端推送来消息时
        var old_msg = ''
        socket.on('new_msg', function (msg) {
            console.log("收到消息：" + msg);
            if(msg.indexOf('==over==error')!=-1){
                msg = msg.replace('==over==error', '')
                var tip = msg=='' ? old_msg : msg;
                $('#loading').html('执行失败：'+tip);
                $('#btn').show();
                parent.setFinish(0);
            }else if(msg.indexOf('==over==success')!=-1 || msg.indexOf('==over==')!=-1){
                msg = msg.replace('==over==success', '').replace('==over==', '')
                $('#loading').html("执行完成");
                $('#btn').show();
                parent.setFinish(1);
            }
            if (msg){
                term.write(msg)
                old_msg = msg
            }
        });
    })

    function closeBox(){
        parent.layer.closeAll()
    }

</script>
{/block}